package com.sdonkey.score.controller;

import com.github.pagehelper.Page;
import com.sdonkey.score.bean.UserMsgBean;
import com.sdonkey.score.service.IUserMsgService;
import com.sdonkey.score.util.StaticVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户消息模块
 *
 * @author ZhaoShihao
 * @version 1.0
 */
@Controller
@RequestMapping(value = "/msg", method = RequestMethod.POST)
public class UserMsgController {
    @Autowired
    private IUserMsgService userMsgService;

    /**
     * 获取指定用户信息列表
     *
     * @param pageNum 页面
     * @return
     */
    @RequestMapping("/ajaxGetUserMsg")
    @ResponseBody
    public Map<String, Object> ajaxGetUserMsg(Integer pageNum, HttpSession session) {
        Integer id = (Integer) session.getAttribute("id");
        if (id != null) {
            if (pageNum == null) {
                pageNum = 1;
            }
            Page<UserMsgBean> msgByUserId = userMsgService.getMsgByUserId(id, pageNum, StaticVariable.msg_page_num);
            Map<String, Object> map = new HashMap<>();
            map.put("pageNum", msgByUserId.getPageNum());
            map.put("pages", msgByUserId.getPages());
            map.put("msg", msgByUserId.getResult());
            return map;
        }
        return null;
    }

    /**
     * 获取指定用户消息未读条数
     *
     * @return
     */
    @RequestMapping("/ajaxGetUnread")
    @ResponseBody
    public Integer ajaxGetUnread(HttpSession session) {
        Integer id = (Integer) session.getAttribute("id");
        return userMsgService.getMSgNumUnRead(id);
    }

    /**
     * 获取指定信息详情
     *
     * @param msgId 信息id
     * @return
     */
    @RequestMapping("/ajaxShowMsg")
    @ResponseBody
    public UserMsgBean ajaxShowMsg(Integer msgId) {
        return userMsgService.getMsgById(msgId);
    }
}
